<template>
    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" v-model="open" width="680px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row>
                <el-col :span="24">
                    <el-form-item label="上级分类">
                        <el-select v-model="form.parentName" placeholder="请选择上级分类" clearable>
                            <el-option :value="parentCategoryData" style="height: auto;">
                                <el-tree :data="categoryOptions"
                                         :props="categoryTreeProps"
                                         node-key="categoryId"
                                         accordion
                                         highlight-current
                                         @node-click="selectCategoryParentId"
                                ></el-tree>
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="分类名称" prop="categoryName">
                        <el-input v-model="form.categoryName" placeholder="请输入分类名称"/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {addDeviceCategory,updateDeviceCategory} from '/src/api/device/category'

    export default {
        name: "update",
        emits: ['dialogStatusChanged'],
        props: {
            open: Boolean,
            title: String,
            form: Object,
            categoryOptions: Array,
        },
        data() {
            return {
                categoryTreeProps: {
                    children: 'children',
                    label: 'categoryName'
                },
                parentCategoryData: {},
                // 表单校验
                rules: {
                    categoryName: [
                        {required: true, message: "分类名称不能为空", trigger: "blur"}
                    ],
                }
            }
        },
        methods: {
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.categoryId != undefined) {
                            updateDeviceCategory(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.close();
                            });
                        } else {
                            addDeviceCategory(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.close();
                            });
                        }
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            },
            //选择上级菜单
            selectCategoryParentId(data, node, nodeData) {
                this.parentCategoryData = data
                this.form.parentName = data.categoryName
                this.form.parentId = data.categoryId
            },
        },
    }
</script>

<style scoped>

</style>
